package se.sll.itintegration.catalogue.product.entity;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 *
 * @author khaleddaham
 */
@SuppressWarnings("PMD.UnusedPrivateField")
@Entity
@Data
@EqualsAndHashCode(of = {"code", "codeSystem"})
@ToString(exclude = {"codeSystem", "products"})
@NamedQueries({
    @NamedQuery(name = CodeEntity.QUERY_FINDBY_OID_AND_CODE, query = "SELECT ce FROM CodeEntity ce WHERE ce.code = :code AND ce.codeSystem.oid = :oid"),
    @NamedQuery(name = CodeEntity.QUERY_FINDBY_OID, query = "SELECT ce FROM CodeEntity ce WHERE ce.codeSystem.oid = :oid"),
    @NamedQuery(name = CodeEntity.QUERY_DISPLAYNAME_LIKE, query = "SELECT ce FROM CodeEntity ce WHERE ce.displayName LIKE :displayname"),
    @NamedQuery(name = CodeEntity.QUERY_DESCRIPTION_LIKE, query = "SELECT ce FROM CodeEntity ce WHERE ce.description LIKE :description")
})
@Table(name = "code", uniqueConstraints =
        @UniqueConstraint(columnNames = {"code", "codeSystem"}))
public class CodeEntity implements Serializable {

    public static final String QUERY_DESCRIPTION_LIKE = "CodeEntity.QUERY_DESCRIPTION_LIKE";
    public static final String QUERY_DISPLAYNAME_LIKE = "CodeEntity.QUERY_DISPLAYNAME_LIKE";
    public static final String QUERY_FINDBY_OID_AND_CODE = "CodeEntity.QUERY_FINDBY_OID_AND_CODE";
    public static final String QUERY_FINDBY_OID = "CodeEntity.QUERY_FINDBY_OID";
    @Id
    @GeneratedValue
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private Long id;
    @NotNull
    private String code;
    @NotNull
    private String displayName;
    @Column(length = 1024)
    private String description;

    @ManyToOne()
    @JoinColumn(name = "codeSystem", nullable = false)
    private CodeSystemEntity codeSystem;
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "codes")
    private Set<ProductEntity> products;
    
}
